package com.wb3.meta.common.mybatis;

import com.github.pagehelper.Page;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


/**
 * MyBatis 分页调用JqueryTable的插件转换工具类
 */
public class DataTableUtil {
    private static final Logger logger = LoggerFactory.getLogger(DataTableUtil.class);

    /**
     * 此方法试用于  com.github 插件
     * ron
     * 2016-03-04
     *
     * @return
     * @throws Exception
     */
    public static  DataTables changePage(Page page) {

        DataTables dataTable = new DataTables();
        try {
            //获取page对象属性,此处主要是获取 前端 datatable 传递的惨呼
            //把数据结果集根据page对象的属性设置为DataTables对象中
            if (null == page) {//为空时按照默认属性进行设置
                dataTable.setRecordsFiltered(0);
                dataTable.setRecordsTotal(0);
//                dataTable.setLength(CommonConstant.PAGE_SIZE);
//                dataTable.setStart(0);
//                dataTable.setDraw(0);
                dataTable.setPageNum(0);
                dataTable.setPageSize(0);
            } else {
                dataTable.setRecordsFiltered(page.getTotal());
                dataTable.setRecordsTotal(page.getTotal());
//                dataTable.setLength(page.getPageSize());
//                dataTable.setStart(page.getStartRow());
                dataTable.setPageNum(page.getPageNum());
                dataTable.setPageSize(page.getPageSize());
                dataTable.setData(page.getResult());
            }
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("MyBatis 分页调用JqueryTable的插件转换失败，请检查" + e.getMessage());
        }

        return dataTable;
    }


}
